import { _mallStore } from "@/store/config";

// 重置 表单
export const resetBuyLimit = (ruleForm: ObjValueAny) => {
    ruleForm.levelIdsChecked = false;
    ruleForm.crowdTypeChecked = false;
    ruleForm.premium_exclusive = 2;
    ruleForm.levelIds = [];
    ruleForm.denyGroups = { id: '', count: 0, name: '' };
    ruleForm.crowdType = '';
}
// 会员参与/领取/购买/权限 ===============================================================
// 表单
export const getBuyLimitRuleForm = (row: ObjValueAny, ruleForm: ObjValueAny) => {
    // 会员领取权限 会员等级
    if (row.level_ids?.length) {
        ruleForm.levelIds = row.level_ids.map((row: ObjValueAny) => {
            return row.id
        })
        ruleForm.levelIdsChecked = true;
    }
    // 会员领取权限 指定不可领取的人群
    if (row.deny_groups?.length) {
        ruleForm.crowdTypeChecked = true;
        const rowDenyGroups = row.deny_groups[0];
        ruleForm.crowdType = !!rowDenyGroups.filename ? 2 : 1;
        ruleForm.denyGroups = rowDenyGroups;
    }
}

const buyLimitUnit: string = '购买';
// 详情
export const getBuyLimitDetail = (row: ObjValueAny = {}, unit: string = buyLimitUnit) => {
    if (+row.participation_rules === 1) {
        row.levelText = '';
        if (row.level_ids?.length) {
            let _levelText: string[] = row.level_ids.map((ele: ObjValueAny) => ele.name);
            row.levelText = `仅限 ${_levelText.join('、')} 可${unit}`;
        }
        if (row.deny_groups?.length) {
            for (let ele of row.deny_groups) {
                ele.text = `${ele.filename || ele.name} - ${ele.count}人不可${unit}`;
            }
        }
        row.premiumExclusiveText = +row.premium_exclusive === 1 ? `仅限${_mallStore.paidMemberInfo.nickname}可${unit}` : '';
    } else {
        row.levelText = `全部会员可${unit}`;
    }
}

// 预览
export const getBuyLimitPreview = (data: ObjValueAny, unit: string = buyLimitUnit) => {
    let _levelTexts: string[] = [];
    if (data.premium_exclusive === 1) {
        _levelTexts.push(_mallStore.paidMemberInfo.nickname);
    }
    if (data.level_ids && data.level_ids.length) {
        let _texts: string[] = data.level_ids.map((row: ObjValueAny) => row.name);
        _levelTexts.push(_texts.join('、'));
    }
    return _levelTexts.length ? `仅限${_levelTexts.join(';')}${unit}` : '';
}


// 会员领取权限
export const buyLimitLevelLimitRule = (value: number, callback: Function, ruleForm: ObjValueAny, text: string = buyLimitUnit) => {
    if (+value === 1 && !ruleForm.levelIdsChecked && !ruleForm.crowdTypeChecked && ruleForm.premium_exclusive !== 1) {
        return callback(new Error(`请选择${text}权限`));
    }
    callback()
}

// 会员等级 验证
export const buyLimitLevelIdsRule = (value: ObjValueAny[], callback: Function, ruleForm: ObjValueAny) => {
    if (ruleForm.levelIdsChecked && !value.length) {
        return callback(new Error('请选择会员等级'));
    }
    callback()
}

// 人群包 验证
export const buyLimitDenyGroupsRule = (value: ObjValueAny, callback: Function, ruleForm: ObjValueAny) => {
    if (!value.id) {
        if (ruleForm.crowdType === 2) {
            return callback(new Error('请上传人群包'));
        }
        if (ruleForm.crowdType === 1) {
            return callback(new Error('请选择人群包'));
        }
    }
    callback()
};